package com.cityos.android.mystudynestedscroll.common.utils;

import android.util.Log;

import androidx.core.util.Consumer;


import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;


/**
 * @author hailong
 * @date 2023年06月21日 11:50
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
public class LoggerUtils {

    private static final String LOG_START = "► 其实，所谓真正的告别，没有古道长亭，也没有劝君更尽一杯酒，也就是在这样一个正午之后，有些人、有些事情就留在回忆里了 ◄\n\n";
    private static final String LOG_END   = "卐 终有告别的一天，各位怀念的也不是那个时候旖旎的风光，而是风光中经历过这件事情的每一个人，难以忘怀的也不是那个时候的光阴，而是自己青春时期的热爱。卐 \n\n ";
    private static final String TAG       = "hailong";

    private static boolean printLog = true;
    private static String  host     = "";

    public static void initialize(boolean printLog, String host) {
        LoggerUtils.printLog = printLog;
        LoggerUtils.host = host;
    }

    public static boolean printLog() {
        return printLog;
    }

    public static void justE(String srcLog) {
        if (printLog) {
            Log.e(TAG, srcLog);
        }
    }

    public static void justE(String srcLog, String tag) {
        if (printLog) {
            Log.e(tag, srcLog);
        }
    }

    public static void e(String srcLog) {
        if (printLog) {
            final String logContent = preHandle(sb -> {
                formattingStr(srcLog, sb);
            });
            Log.e(TAG, logContent);
        }
    }


    public static void e(String srcLog, String tag) {
        if (printLog) {
            final String logContent = preHandle(sb -> {
                sb.append(tag).append("===========>\n");
                formattingStr(srcLog, sb);
            });
            Log.e(TAG, logContent);
        }
    }


    public static void e(String srcLog, String... tags) {
        if (printLog) {
            final String logContent = preHandle(sb -> {
                for (int i = 0; i < tags.length; i++) {
                    sb.append(tags[i]);
                    if (i != tags.length - 1) {
                        sb.append(",");
                    }
                }
                formattingStr(srcLog, sb);
            });
            Log.e(TAG, logContent);
        }
    }


    public static void v(String srcLog) {
        if (printLog) {
            final String logContent = preHandle(sb -> {
                formattingStr(srcLog, sb);
            });
            Log.v(TAG, logContent);
        }
    }

    public static void v(String srcLog, String tag) {
        if (printLog) {
            final String logContent = preHandle(sb -> {
                sb.append(tag).append("===========>\n");
                formattingStr(srcLog, sb);
            });
            Log.v(TAG, logContent);
        }
    }


    public static void d(String srcLog) {
        if (printLog) {
            final String logContent = preHandle(sb -> {
                formattingStr(srcLog, sb);
            });
            Log.d(TAG, logContent);
        }
    }


    public static void d(String srcLog, String tag) {
        if (printLog) {
            final String logContent = preHandle(sb -> {
                sb.append(tag).append("===========>\n");
                formattingStr(srcLog, sb);
            });
            Log.d(TAG, logContent);
        }
    }


    public static void i(String srcLog) {
        if (printLog) {
            final String logContent = preHandle(sb -> {
                formattingStr(srcLog, sb);
            });
            Log.i(TAG, logContent);
        }
    }


    public static void i(String srcLog, String tag) {
        if (printLog) {
            final String logContent = preHandle(sb -> {
                sb.append(tag).append("===========>\n");
                formattingStr(srcLog, sb);
            });
            Log.i(TAG, logContent);

        }
    }


    public static void printException(Throwable e) {
        if (printLog) {
            e.printStackTrace();
        }
    }

    public static void printExceptionAndOtherHint(Throwable e, String otherMsg) {
        if (printLog) {
            Log.e(TAG, otherMsg);
            e.printStackTrace();
        }
    }


    public static void printException(Throwable e, String apiTag) {
        if (printLog) {
            final String logContent = preHandle(sb -> sb.append(apiTag).append(" ").append(e.getMessage()));
            Log.e(TAG, logContent);
            e.printStackTrace();
        }
    }


    /**
     * 处理日志
     *
     * @param consumer
     * @return
     */
    private static String preHandle(Consumer<StringBuilder> consumer) {
        final StringBuilder sb = new StringBuilder();
        sb.append(LOG_START);
        consumer.accept(sb);
        sb.append(LOG_END);
        return sb.toString();
    }

    private static void formattingStr(String srcLog, StringBuilder sb) {
        if (srcLog.length() > 300) {
            int s = 0, e = 300;
            while (s < srcLog.length()) {
                String substring = srcLog.substring(s, e);
                sb.append(substring).append("\n");
                s = e;
                e = Math.min(e + 300, srcLog.length());
            }
        } else {
            sb.append(srcLog);
        }
        sb.append("\n\n");
    }


}
